package it.abc.sicsic.security;

import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;


public class SecurityService {
    
    private Subject currentUser = null;
    private static  Logger log = Logger.getLogger(SecurityService.class);
    
    
    public SecurityService() {
        Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
        SecurityManager securityManager = factory.getInstance();
        SecurityUtils.setSecurityManager(securityManager);
        currentUser = SecurityUtils.getSubject();

    }
    
    public  boolean login( String user, String psw ){
        log.info("Requesting login for user=[" + user + "] psw=[" + psw + "]");
        
        UsernamePasswordToken token = new UsernamePasswordToken( user , psw );
        token.setRememberMe(false);
        try {
            currentUser.login(token);
        } catch (UnknownAccountException uae) {
            log.info("There is no user with username of " + token.getPrincipal());
            return false;
        } catch (IncorrectCredentialsException ice) {
            log.info("Password for account " + token.getPrincipal() + " was incorrect!");
            return false;
        } catch (LockedAccountException lae) {
            log.info("The account for username " + token.getPrincipal() + " is locked.  " +
                    "Please contact your administrator to unlock it.");
            return false;
        }
        // ... catch more exceptions here (maybe custom ones specific to your application?
        catch (AuthenticationException ae) {
            log.error("Error", ae);
            return false;
        }
        
        return true;

    }
    
    
    public static void main (String[] args){
        
        Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
        SecurityManager securityManager = factory.getInstance();
       SecurityUtils.setSecurityManager(securityManager);
       Subject currentUser = SecurityUtils.getSubject();

       // Do some stuff with a Session (no need for a web or EJB container!!!)
       Session session = currentUser.getSession();
       session.setAttribute("someKey", "aValue");
       
       String value = (String) session.getAttribute("someKey");
       
       if (value.equals("aValue")) {
           log.info("Retrieved the correct value! [" + value + "]");
       }

       // let's login the current user so we can check against roles and permissions:
       if (!currentUser.isAuthenticated()) {
           UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");
           token.setRememberMe(true);
           try {
               currentUser.login(token);
           } catch (UnknownAccountException uae) {
               log.info("There is no user with username of " + token.getPrincipal());
           } catch (IncorrectCredentialsException ice) {
               log.info("Password for account " + token.getPrincipal() + " was incorrect!");
           } catch (LockedAccountException lae) {
               log.info("The account for username " + token.getPrincipal() + " is locked.  " +
                       "Please contact your administrator to unlock it.");
           }
           // ... catch more exceptions here (maybe custom ones specific to your application?
           catch (AuthenticationException ae) {
               //unexpected condition?  error?
           }
       }

       //say who they are:
       //print their identifying principal (in this case, a username):
       log.info("User [" + currentUser.getPrincipal() + "] logged in successfully.");

       //test a role:
       if (currentUser.hasRole("schwartz")) {
           log.info("May the Schwartz be with you!");
       } else {
           log.info("Hello, mere mortal.");
       }

       //test a typed permission (not instance-level)
       if (currentUser.isPermitted("lightsaber:weild")) {
           log.info("You may use a lightsaber ring.  Use it wisely.");
       } else {
           log.info("Sorry, lightsaber rings are for schwartz masters only.");
       }

       //a (very powerful) Instance Level permission:
       if (currentUser.isPermitted("winnebago:drive:eagle5")) {
           log.info("You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'.  " +
                   "Here are the keys - have fun!");
       } else {
           log.info("Sorry, you aren't allowed to drive the 'eagle5' winnebago!");
       }

       //all done - log out!
       currentUser.logout();

    }
        
}
